﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CitrixSystemVerifier
{
    class RegistryValue
    {
        public enum PartType { Key, Value };
        public enum OperationType { NoOp, CompareVersion, SetVariable, SetDirVariable, SetFlagVariable, CompareValue, SetContextVariable, TestAndSetContextVariable };

        public RegistryValue()
        {
            m_Found = false;
            m_Part = PartType.Key;
            m_ValueName = null;
            m_Message = null;
            m_Operation = OperationType.NoOp;
            m_Argument1 = null;
            m_Context = null;
            m_Required = true;
        }

        public RegistryValue(PartType Part, string ValueName, string Message)
        {
            m_Found = false;
            m_Part = Part;
            m_ValueName = ValueName;
            m_Message = Message;
            m_Operation = OperationType.NoOp;
            m_Argument1 = null;
            m_Context = null;
            m_Required = true;
        }

        public RegistryValue(PartType Part, string ValueName, string Message, OperationType Operation)
        {
            m_Found = false;
            m_Part = Part;
            m_ValueName = ValueName;
            m_Message = Message;
            m_Operation = Operation;
            m_Argument1 = null;
            m_Context = null;
            m_Required = true;
        }

        public RegistryValue(PartType Part, string ValueName, string Message, OperationType Operation, string Argument)
        {
            m_Found = false;
            m_Part = Part;
            m_ValueName = ValueName;
            m_Message = Message;
            m_Operation = Operation;
            m_Argument1 = Argument;
            m_Context = null;
            m_Required = true;
        }

        public RegistryValue(PartType Part, string ValueName, string Message, OperationType Operation, string Argument, string Context)
        {
            m_Found = false;
            m_Part = Part;
            m_ValueName = ValueName;
            m_Message = Message;
            m_Operation = Operation;
            m_Argument1 = Argument;
            m_Context = Context.Replace('+', ',');
            m_Required = true;
        }

        public bool IsValid ()
        {
            switch (m_Operation)
            {
                case OperationType.TestAndSetContextVariable:
                    {
                        if (!string.IsNullOrEmpty(m_Argument1) && !string.IsNullOrEmpty(m_Argument2))
                            return true;
                        else
                            return false;
                    }
                default:
                    {
                        if (!string.IsNullOrEmpty(m_ValueName))
                            return true;
                    }
                    break;
            }
            return false;
        }

        public static PartType ParsePartType(string parttype)
        {
            switch (parttype.Trim())
            {
                case "Key":
                    return PartType.Key;
                case "Value":
                    return PartType.Value;
            }
            throw (new SystemException());
        }

        public static OperationType ParseOperationType(string operationtype)
        {
            switch (operationtype.Trim())
            {
                case "":
                case "NoOp":
                    return OperationType.NoOp;
                case "CompareVersion":
                    return OperationType.CompareVersion;
                case "SetVariable":
                    return OperationType.SetVariable;
                case "SetDirVariable":
                    return OperationType.SetDirVariable;
                case "SetFlagVariable":
                    return OperationType.SetFlagVariable;
                case "CompareValue":
                    return OperationType.CompareValue;
                case "SetContextVariable":
                    return OperationType.SetContextVariable;
                case "TestAndSetContextVariable":
                    return OperationType.TestAndSetContextVariable;
            }
            throw (new SystemException());
        }

        private bool m_Found;
        public bool Found
        {
            get { return m_Found; }
            set { m_Found = value; }
        }

        private string m_ValueName;
        public string ValueName
        {
            get { return m_ValueName; }
            set { m_ValueName = value; }
        }

        private PartType m_Part;
        internal PartType Part
        {
            get { return m_Part; }
            set { m_Part = value; }
        }

        private OperationType m_Operation;
        public OperationType Operation
        {
            get { return m_Operation; }
            set { m_Operation = value; }
        }

        private string m_Argument1;
        public string Argument1
        {
            get { return m_Argument1; }
            set { m_Argument1 = value; }
        }

        private string m_Argument2;
        public string Argument2
        {
            get { return m_Argument2; }
            set { m_Argument2 = value; }
        }

        private string m_Context;
        public string Context
        {
            get { return m_Context; }
            set { m_Context = value.Replace('+', ','); }
        }

        private string m_Message;
        public string Message
        {
            get { return m_Message; }
            set { m_Message = value; }
        }

        private bool m_Required;
        public bool Required
        {
            get { return m_Required; }
            set { m_Required = value; }
        }

    }
}
